<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>注册</title>
    <link href="../static/css/register.css" rel="stylesheet">
    <script src="../static/js/jquery.js"></script>
</head>

<body class="bg-gradient-primary">

<div class="container">

    <div class="card o-hidden border-0 shadow-lg my-5">
        <div class="card-body p-0">
            <!-- Nested Row within Card Body -->
            <div class="row">
                <div class="col-lg-5 d-none d-lg-block bg-register-image"></div>
                <div class="col-lg-7">
                    <div class="p-5">
                        <div class="text-center">
                            <h1 class="h4 text-gray-900 mb-4">创建账号</h1>
                        </div>
                        <form class="user">
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                学号/工号/账号:
                                <input type="text" class="form-control form-control-user" id="register_id"
                                       name="register_id" placeholder="注册账号">
                            </div>
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                姓名:
                                <input type="text" class="form-control form-control-user" id="register_name"
                                       name="register_name" placeholder="用户名">
                            </div>
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                密码:
                                <input type="password" class="form-control form-control-user" id="register_pwd"
                                       name="register_pwd" placeholder="注册密码">
                            </div>
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                确认密码:
                                <input type="password" class="form-control form-control-user" id="register_pwd_con"
                                       name="register_pwd_con" placeholder="确认密码">
                            </div>
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                性别:
                                <label>
                                    <input type="radio" name="sex" value="男" checked>男
                                </label>
                                <label>
                                    <input type="radio" name="sex" value="女">女
                                </label>
                            </div>
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                院系:
                                <select id="faculty">
                                    <option selected>计算机科学与技术</option>
                                    <option>经济学院</option>
                                    <option>物理学院</option>
                                    <option>土木工程</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <span style="color: red;font-size: 12px;">*</span>
                                职务:
                                <select id="user_priority">
                                    <option value="0" selected>学生</option>
                                    <option value="1">教师</option>
                                </select>
                            </div>
                            <font color="red"></font>
                            <button type="button" class="btn btn-primary btn-user btn-block"
                                    onclick="register()">注册
                            </button>
                        </form>
                        <div class="text-center">
                            <a class="small" href="login.html">已有账号? 登录!</a></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    // $(function () {
    //     $.ajax({
    //         type: "post",
    //         url: "http://localhost:8080/user/getCurrentUser",
    //         success: function (result) {
    //             if (result.code != '200') {
    //                 alert("请先登录账号！");
    //                 window.location.href = "login.html";
    //             }
    //         }
    //     });
    // });

    function register() {
        let register_id = $("#register_id").val();
        let register_name = $("#register_name").val();
        let register_pwd = $("#register_pwd").val();
        let register_pwd_con = $("#register_pwd_con").val();
        let register_sex = $("input[name='sex']:checked").val();
        let register_faculty = $("#faculty option:selected").text();
        let register_priority = $("#user_priority option:selected").val();

        let re = /[1-9][0-9]*/;//判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/
        if (!re.test(register_id)) {
            alert("请输入正确的id");
        }
        if (register_pwd === register_pwd_con) {
            let data = {
                "userid": register_id,
                "username": register_name,
                "password": register_pwd,
                "sex": register_sex,
                "faculty": register_faculty,
                "priority": register_priority
            };
            $.post('http://localhost:8080/user/userRegister', data, function (result) {
                if (result.code == 200) {
                    alert(result.msg);
                    window.location.href = "login.html";
                } else {
                    alert(result.msg);
                }
            })
        } else {
            alert("两次密码不一致！");
        }
    }
</script>
</body>
</html>